package com.boost.socialNetwork.client;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.CheckBox;

public class Login extends LayoutPanel {
	
	private Label head;
	private Label lblNewLabel_1;
	static private TextBox username;
	static private PasswordTextBox password;
	private Button signIn;
	private Label userfield;
	private Label passField;	
		
	private final UserServiceAsync greetService = GWT
	.create(UserService.class);

	//private LayoutPanel this;	
	
	public Login() {
		super();
		System.out.println("Da5al el login");
		//HistoryManager.newHistory("PageLogin");
		
		head = new Label("Login Page");				
		this.setSize("495px", "336px");
		
		Label lblNewLabel = new Label("Username");
		this.add(lblNewLabel);
		this.setWidgetLeftWidth(lblNewLabel, 34.0, Unit.PX, 71.0, Unit.PX);
		this.setWidgetTopHeight(lblNewLabel, 73.0, Unit.PX, 16.0, Unit.PX);		
		lblNewLabel.setStyleName("gwt-label");
		
		lblNewLabel_1 = new Label("Password");
		this.add(lblNewLabel_1);
		this.setWidgetLeftWidth(lblNewLabel_1, 34.0, Unit.PX, 68.0, Unit.PX);
		this.setWidgetTopHeight(lblNewLabel_1, 112.0, Unit.PX, 16.0, Unit.PX);
		lblNewLabel_1.setStyleName("gwt-label");
		
		username = new TextBox();
		username.setStyleName("gwt-username");
		this.add(username);
		this.setWidgetLeftWidth(username, 120.0, Unit.PX, 155.0, Unit.PX);
		this.setWidgetTopHeight(username, 73.0, Unit.PX, 28.0, Unit.PX);
		
		username.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				username.setText("");
				username.setStyleName("gwt-TextBox");
			}
		});
		username.setText("username");
						
		
		password = new PasswordTextBox();
		password.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				password.setText("");				
			}
		});
		password.setText("password");
		this.add(password);
		this.setWidgetLeftWidth(password, 120.0, Unit.PX, 155.0, Unit.PX);
		this.setWidgetTopHeight(password, 112.0, Unit.PX, 28.0, Unit.PX);
		
		userfield = new Label("field required");
		userfield.setVisible(false);
		this.add(userfield);
		this.setWidgetLeftWidth(userfield, 281.0, Unit.PX, 105.0, Unit.PX);
		this.setWidgetTopHeight(userfield, 73.0, Unit.PX, 16.0, Unit.PX);
		userfield.setStyleName("gwt-validation");
		
		
		passField = new Label("field required");
		passField.setVisible(false);
		this.add(passField);
		this.setWidgetLeftWidth(passField, 281.0, Unit.PX, 85.0, Unit.PX);
		this.setWidgetTopHeight(passField, 112.0, Unit.PX, 16.0, Unit.PX);
		passField.setStyleName("gwt-validation");
		
		final InlineLabel errorFail = new InlineLabel("");
		errorFail.setStyleName("gwt-validation");
		this.add(errorFail);
		this.setWidgetLeftWidth(errorFail, 140.0, Unit.PX, 90.0, Unit.PX);
		this.setWidgetTopHeight(errorFail, 40.0, Unit.PX, 16.0, Unit.PX);
		
		signIn = new Button("Sign In");
		signIn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// validations
				if (username.getText().equals("")) {
					userfield.setVisible(true);
				}
				if (password.getText().equals("")) {
					passField.setVisible(true);
				}
				if (!(username.getText().equals("")) && !(password.getText().equals(""))) {
					greetService.authenticate(username.getText(), password.getText(),
							new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									
								}

								@Override
								public void onSuccess(String result) {									
									if (result.equals("200 OK")) {
									//	RootPanel.get("post").clear();
									//	RootPanel.get("head").clear();
										
									//	Home home = new Home();
										HistoryManager.setUsername(username.getText());
										HistoryManager.newHistory("PageHome");
										
									//	RootPanel.get("post").add(home);

								//		home.setHead(username.getText());
								//		RootPanel.get("head").add(home.getHead());
								//		SideBar side = new SideBar();
								//		RootPanel.get("sidebar").add(side);
									} else {
										Window.alert("Please check username and password");
									}
									
								}
							});
					
					
					//					Mamdouh
//					userService.authenticateJDO(username.getText(), password.getText(),
//							new AsyncCallback<String>() {
//								
//								@Override
//								public void onSuccess(String result) {
//									if (result.equals("200 OK")) {
//										RootPanel.get("post").clear();
//										RootPanel.get("head").clear();
//										Home home = new Home();
//										RootPanel.get("post").add(home);
//										home.setHead(username.getText());
//										RootPanel.get("head").add(home.getHead());
//										SideBar side = new SideBar();
//										RootPanel.get("sidebar").add(side);
//									}
//								}
//								
//								@Override
//								public void onFailure(Throwable caught) {
//									// TODO Auto-generated method stub
//									
//								}
//							});
				}
			}
		});
		this.add(signIn);
		this.setWidgetLeftWidth(signIn, 145.0, Unit.PX, 85.0, Unit.PX);
		this.setWidgetTopHeight(signIn, 171.0, Unit.PX, 39.0, Unit.PX);
		
		CheckBox chckbxRememberMe = new CheckBox("Remember Me");
		add(chckbxRememberMe);
		setWidgetLeftWidth(chckbxRememberMe, 130.0, Unit.PX, 124.0, Unit.PX);
		setWidgetTopHeight(chckbxRememberMe, 146.0, Unit.PX, 19.0, Unit.PX);
		
		Label lblRegisterNow = new Label("register now");
		lblRegisterNow.setStyleName("gwt-LabelLink");
		lblRegisterNow.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				HistoryManager.newHistory("PageReg");
				//Registration reg = new Registration();
				//RootPanel.get("post").clear();
				//RootPanel.get("head").clear();
				//RootPanel.get("post").add(reg);
				//RootPanel.get("head").add(reg.getHead());
				
			}
		});
		add(lblRegisterNow);
		setWidgetLeftWidth(lblRegisterNow, 150.0, Unit.PX, 80.0, Unit.PX);
		setWidgetTopHeight(lblRegisterNow, 216.0, Unit.PX, 19.0, Unit.PX);
		
		/*HistoryManager.addValueChangeHandler(new ValueChangeHandler<String>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				HistoryManager.HistoryChange(event);
				
			}
		});*/
		System.out.println("login prob");
	}

	public Button getSignIn() {
		return signIn;
	}
	public Label getHead() {	
		return head;
	}
	
	public static void reset(){
		username.setText("");
		password.setText("");
		
	}
}
